package com.imooc.service.impl;

import com.imooc.common.constants.ResultCodeConstants;
import com.imooc.common.utils.ExceptionProcessorUtils;
import com.imooc.converter.CategoryConverter;
import com.imooc.dto.category.CategoryDto;
import com.imooc.dto.category.CategoryListResponse;
import com.imooc.mapper.CategoryMapper;
import com.imooc.mapper.CategoryMapperCustom;
import com.imooc.pojo.Category;
import com.imooc.pojo.vo.CategoryVO;
import com.imooc.service.ICategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @ClassName: CategoryServiceImpl
 * @Description:
 * @author: limingxing
 * @Date: 2019-12-25 19:38
 */
@Slf4j
@Service
public class CategoryServiceImpl implements ICategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private CategoryConverter categoryConverter;
    @Autowired
    private CategoryMapperCustom categoryMapperCustom;


    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public CategoryListResponse queryAllRootLevel() {
        CategoryListResponse response = new CategoryListResponse();
        Example example = new Example(Category.class);
        Example.Criteria criteria = example.createCriteria();
        try {
            criteria.andEqualTo("type", 1);
            List<Category> result = categoryMapper.selectByExample(example);
            List<CategoryDto> categoryDtoList = categoryConverter.category2List(result);
            response.setList(categoryDtoList);
        }catch (Exception e){
            log.error("CategoryServiceImpl.queryAllRootLevel Occur Exception : [{}]" , e);
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        response.setCode(ResultCodeConstants.SUCCESS.getCode());
        response.setMsg(ResultCodeConstants.SUCCESS.getMessage());
        return response;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<CategoryVO> getSubCatList(Integer rootCatId) {
        log.info("Begin CategoryServiceImpl.getSubCatList rootCatId: [{}]",rootCatId);
        return categoryMapperCustom.getSubCatList(rootCatId);
    }
}
